package com.alibaba.citrus.cr.mn.order.center.facade.service.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.service.OrderBizDomainService;
import com.alibaba.citrus.cr.mn.order.center.forecastorder.ForecastOrderBizDomainService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.model.dto.ForecastOrderBizQueryResponse;
import com.epoch.app.mnordercenter.model.dto.OrderBizQueryResponse;
import com.epoch.app.mnordercenter.model.dto.RelationOrderSDO;
import com.epoch.app.mnordercenter.orderquery.dto.QuerySingleRequest;
import com.epoch.app.mnordercenter.orderquery.service.OrderQueryService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhoubc
 * @date 2023/5/28 15:17
 */
@Primary
@Component
public class OrderQueryServiceImpl implements OrderQueryService {

    @Resource
    private OrderBizDomainService orderBizDomainService;
    @Resource
    private ForecastOrderBizDomainService forecastOrderBizDomainService;

    @Override
    public Result<OrderBizQueryResponse> querySingle(QuerySingleRequest querySingleRequest) {
        Result<OrderBizQueryResponse> orderResponse = orderBizDomainService.queryOrderDetail(querySingleRequest.getId());
        OrderBizQueryResponse orderBizQueryResponse = orderResponse.getResult();
        if(StringUtils.isNotBlank(orderBizQueryResponse.getFrontOrderId())){
            Result<ForecastOrderBizQueryResponse> forecastOrderResponse = forecastOrderBizDomainService.queryForecastOrderDetail(orderBizQueryResponse.getFrontOrderId());
            ForecastOrderBizQueryResponse forecastOrderBizQueryResponse = forecastOrderResponse.getResult();
            RelationOrderSDO relationOrderSDO = new RelationOrderSDO();
            relationOrderSDO.setId(forecastOrderBizQueryResponse.getId());
            relationOrderSDO.setBizId(forecastOrderBizQueryResponse.getOrderBizId());
            relationOrderSDO.setOrderTypeName(forecastOrderBizQueryResponse.getOrderTypeName());
            relationOrderSDO.setOrderTime(forecastOrderBizQueryResponse.getGmtCreate());
            relationOrderSDO.setCreateName(forecastOrderBizQueryResponse.getCreatorName());
            relationOrderSDO.setRemark(forecastOrderBizQueryResponse.getRemark());
            orderBizQueryResponse.setRelationOrderList(Lists.newArrayList(relationOrderSDO));
        }

        return Result.success(orderBizQueryResponse);
    }
}
